// 67. 二进制求和
// https://leetcode.cn/problems/add-binary/description/
// 简单,给你两个二进制字符串 a 和 b ，
// 以二进制字符串的形式返回它们的和。
// 输入:a = "11", b = "1"
// 输出："100"
// 输入：a = "1010", b = "1011"
// 输出："10101"

#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

class Solution {
public:
    string addBinary(string a, string b) {
        string res;
        int carry = 0;
        int i = a.length()-1;
        int j = b.length()-1;
        for (; carry > 0 || i >= 0 || j >= 0; i--,j--){
            int cnt = carry;
            if(i >= 0)
                cnt += (a[i] - '0');
            if(j >= 0)
                cnt += (b[j] - '0');
            res = to_string(cnt % 2) + res;
            carry = cnt / 2;
        }
        return res;
    }
};

int main() {
  Solution solu;
  string result = solu.addBinary("111", "1");
  cout << result << endl;
  return 0;
}
